#include<stdio.h>
#include<stdlib.h>
#include<string.h>

#define MAXSIZE 1024
/*功能:微型学生信息管理系统
 *
 */


	struct birthday_st
{
	int year;
	int month;
	int day;
};



	struct student_st	
{
	char *name;
	char sex;
	int age;
	float score;
	struct birthday_st birth;
}stu,*p,std[5],tmp[5];

/*信息设置
 *
 */

	void stu_set(struct student_st *p,struct student_st *q)
{
	int i;

	for(i = 0;i < 5;i++)
	{
		(p+i)->name = malloc(strlen((q+i)->name)+1);
		if((p+i)->name == NULL)
			exit(1);
		strcpy((p+i)-> name,(q+i)->name);
		(p+i)-> sex = (q+i)->sex;
		(p+i)->age = (q+i)->age;
		(p+i)->score = (q+i)->score;
		(p+i)->birth.year = (q+i)->birth.year;
		(p+i)->birth.month = (q+i)->birth.month;
		(p+i)->birth.day = (q+i)->birth.day;


	}

}
/*功能:修改学生名字
 *@src:新的名字
 *@n:名字序号
 */

	void stu_change(struct student_st *p,char *src,int n)
{
	free((p+n-1)->name);
	(p+n-1)->name = malloc(strlen(src)+1);
	if((p+n-1) == NULL)
		exit(1);;
	strcpy((p+n-1)->name,src);

}


	void stu_show(struct student_st *p)
{
	int i;
	for(i = 0;i < 5;i++)
	printf("%s %c %d %f  %d-%d-%d\n",(p+i)->name,(p+i)->sex,(p+i)->age,(p+i)->score,(p+i)->birth.year,(p+i)->birth.month,(p+i)->birth.day);

}
/*功能:按学生成绩排序
 *
 *
 */

	void stu_sort(struct student_st *p)
{
	int i,j;
	struct student_st *temp = NULL;
	struct student_st *q[5];
	for(i = 0;i < 5;i++)
		q[i] = &p[i];

	for(i = 0;i < 4;i++)
	{
		for(j = 0;j < 4-i;j++)
		{
			if(q[j]->score < q[j+1]->score)
			{
				temp = q[j];
				q[j] = q[j+1];
				q[j+1] = temp;
			}
		}
	}
	for(i = 0;i < 5;i++)
	printf("%s %c %d %f  %d-%d-%d\n",q[i]->name,q[i]->sex,q[i]->age,q[i]->score,q[i]->birth.year,q[i]->birth.month,q[i]->birth.day);


}

/*功能:菜单选择
 *
 *
 */
	int stu_menu()
{
	int choice;
	int ret;
	printf("\n1,set_student(name sex age score birth)\n2,change_student_name\n3,score sort\n4,show\n\n");
	printf("Please select:(q or Q is Quit)\n");
	ret = scanf("%d",&choice);
	if(ret == 0)
		exit(1);
//	sleep(1);
	return choice;
}



	void stu_func()
{
		
	int i,j;
	int n,choice;
	char name[MAXSIZE];


	while(1)
	{
	
	choice = stu_menu();
	switch(choice)
	{
	case 1:
	{
/*		
	for(i = 0;i < 5;i++)
	{
	tmp[i].name = malloc(MAXSIZE);
	if(tmp[i].name == NULL)
		exit(1);
		scanf("%s%*c%c%d%f",tmp[i].name,&tmp[i].sex,&tmp[i].age,&tmp[i].score);
		scanf("%d%d%d",&tmp[i].birth.year,&tmp[i].birth.month,&tmp[i].birth.day);
		printf("Next\n");
	}
*/
/*功能:随机生成学生信息
 *
 *
 */
	for(i = 0;i < 5;i++)
	{
		tmp[i].name = malloc(MAXSIZE);
		if(tmp[i].name == NULL)
			exit(1);
		snprintf(tmp[i].name,MAXSIZE,"stu%d",i);
		if(i % 2)
		tmp[i].sex = 'w';
		else
			tmp[i].sex = 'm';
		tmp[i].age = 18+rand()%5;
		tmp[i].score = rand()%100;
		tmp[i].birth.year = 1990+rand()%12;
		tmp[i].birth.month = 1+rand()%12;
		tmp[i].birth.day = 1+rand()%30;
	}
	stu_set(std,tmp);	
	break;
	}
	case 2:
	{
	printf("Please input change of student to you want:\n");
	scanf("%d",&n);
	printf("Please enter newname:\n");
	scanf("%s",name);
	stu_change(std,name,n);
	break;
	}
	case 3:
	{
	stu_sort(std);
	break;
	}
	case 4:
	{
	stu_show(std);
	break;
	}
	default :
	{
	exit(1);
	}
	}
	}
}










	int main()
{
	stu_func();
}
